package com.zyh.controller;

import com.zyh.entity.Book;
import com.zyh.entity.Type;
import com.zyh.service.BookService;
import com.zyh.service.TypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private ModelAndView mav;

    @RequestMapping("/selectAllByPageMvc")
    public ModelAndView selectAllByPageMvc(HttpSession httpSession, @Param("curPageStr") String curPageStr,
                                           @RequestParam(value = "nameSearch",defaultValue = "",required = false) String nameSearch,
                                           @RequestParam(value = "priceSearch",defaultValue = "",required = false) Double priceSearch,
                                           @RequestParam(value = "typeSearch",defaultValue = "",required = false) String typeSearch){
        int curPage = 1;
        List<Book> bookList = null;
        if(curPageStr!=null){
            curPage=Integer.parseInt(curPageStr);
        }

        if(nameSearch!=""){
            httpSession.setAttribute("nameSearchSession",nameSearch);
        }
        if(priceSearch!=null){
            httpSession.setAttribute("priceSearchSession",priceSearch);
        }
        if(typeSearch!=""){
            httpSession.setAttribute("typeSearchSession",typeSearch);
        }

        int pageSize = 5;
        int count = bookService.selectCount();
        int total = count%pageSize==0 ? count/pageSize : count/pageSize+1;
        int start = (curPage-1)*pageSize;

        if(httpSession.getAttribute("nameSearchSession")!="" || (String)httpSession.getAttribute("priceSearchSession")!="" || httpSession.getAttribute("typeSearchSession")!=""){
            bookList = bookService.selectBooks(start, pageSize ,(String) httpSession.getAttribute("nameSearchSession") ,(Double) httpSession.getAttribute("priceSearchSession"),(String) httpSession.getAttribute("typeSearchSession"));
        }else{
            bookList = bookService.selectBooks(start, pageSize ,nameSearch ,priceSearch,typeSearch);
            System.err.println(total);
        }

        mav.addObject("curPage",curPage);
        mav.addObject("total",total);
        mav.addObject("bookList",bookList);
        mav.setViewName("/booklist.jsp");
        return mav;
    }
    @RequestMapping("/deletesByIdsMvc")
    public ModelAndView deletesByIdsMvc(int[] ids){
        bookService.deletesByBookId(ids);
        mav.setViewName("redirect:/book/selectAllByPageMvc.do");
        return mav;
    }
    @RequestMapping("/insertMvc")
    public ModelAndView insertMvc(Book book,String typeName){
        Type type = typeService.selectByTypeName(typeName);
        System.err.println(type);
        book.setBookTypeId(type.getTypeId());
        book.setBookGroundingDate(new Date());
        bookService.insert(book);
        mav.setViewName("redirect:/book/selectAllByPageMvc.do");
        return mav;
    }
    @RequestMapping("/deleteByBookIdMvc")
    public ModelAndView deleteByBookIdMvc(Integer bookId){
        bookService.deleteByBookId(bookId);
        mav.setViewName("redirect:/book/selectAllByPageMvc.do");
        return mav;
    }

    @RequestMapping("/selectByBookIdMvc")
    public ModelAndView selectByBookIdMvc(Integer bookId){
        Book book = bookService.selectByBookId(bookId);
        List<Type> typeList = typeService.selectTypes();
        mav.addObject("book",book);
        mav.addObject("typeList",typeList);
        mav.setViewName("/updateBook.jsp");
        return mav;
    }
    @RequestMapping("/updateByBookIdMvc")
    public ModelAndView updateByBookIdMvc(Book book,String typeName){
        Type type = typeService.selectByTypeName(typeName);
        book.setBookTypeId(type.getTypeId());
        book.setBookGroundingDate(new Date());
        bookService.updateByBookId(book);
        mav.setViewName("redirect:/book/selectAllByPageMvc.do");
        return mav;
    }

    @RequestMapping("/refreshMvc")
    public ModelAndView refreshMvc(HttpSession httpSession){
        httpSession.removeAttribute("nameSearchSession");
        httpSession.removeAttribute("priceSearchSession");
        httpSession.removeAttribute("typeSearchSession");
        mav.setViewName("redirect:/book/selectAllByPageMvc.do");
        return mav;
    }

}
